#include "SelectPassDialog.h"
#include "ui_SelectPassDialog.h"
#include "CryptoAgent.h"
#include <QPushButton>

SelectPassDialog::SelectPassDialog(DataProvider* provider, QWidget *parent) :
	QDialog(parent),
	ui(new Ui::SelectPassDialog),
	_provider(provider),
	_type(CipherType::None),
	_pass("")
{
	ui->setupUi(this);

	setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);

	ui->comboBox->insertItem(0,tr("None"),CipherType::None);
	ui->comboBox->insertItem(1,"RC4",CipherType::RC4);
	ui->comboBox->insertItem(2,"DES",CipherType::DES);
	ui->comboBox->insertItem(3,"AES",CipherType::AES);

	ui->checkBox->setChecked(true);

	ui->showPass->setChecked(_provider->settings()->showPass);
	onChangePassView(_provider->settings()->showPass);
	_type = _provider->settings()->userType;

	connect(ui->showPass, SIGNAL(toggled(bool)), SLOT(onChangePassView(bool)));
	connect(ui->comboBox,SIGNAL(currentIndexChanged(int)),SLOT(cipherSelected(int)));
	connect(ui->buttonBox->button(QDialogButtonBox::Ok),SIGNAL(clicked()),SLOT(onOK()));
	connect(ui->checkBox,SIGNAL(toggled(bool)),SLOT(onChangeShowDlg(bool)));

	int index = ui->comboBox->findData(_type);
	if(index != -1){
		ui->comboBox->setCurrentIndex(index);
		cipherSelected(index);

	}
}

SelectPassDialog::~SelectPassDialog()
{
	delete ui;
}

void SelectPassDialog::onChangePassView(bool state)
{
	ui->lineEdit->setEchoMode(!state ? QLineEdit::Password : QLineEdit::Normal);
	_provider->settings()->showPass = state;
}

void SelectPassDialog::cipherSelected(int index)
{
	if(index == -1) return;
	CipherType::Enum type = (CipherType::Enum) ui->comboBox->itemData(index).value<int>();
	ui->lineEdit->setEnabled(type != CipherType::None);
	ui->showPass->setEnabled(type != CipherType::None);
	_type = type;
	_provider->settings()->userType = _type;
}

void SelectPassDialog::onOK()
{
	_pass = ui->lineEdit->text();
}

CipherType::Enum SelectPassDialog::getCipherType()
{
	if(_pass.size() == 0) return CipherType::None;
	return _type;
}

QString SelectPassDialog::getPass()
{
	return _pass;
}

void SelectPassDialog::onChangeShowDlg(bool state)
{
	_provider->settings()->showDlgAtStartup = state;
}
